package com.yupi.springbootinit.manager;

import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.yucongming.dev.client.YuCongMingClient;
import com.yupi.yucongming.dev.common.BaseResponse;
import com.yupi.yucongming.dev.model.DevChatRequest;
import com.yupi.yucongming.dev.model.DevChatResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AiManager {

    //先在application.yml中填写
    // yuapi:
    //   client:
    //     access-key: 你的 access-key
    //     secret-key: 你的 secret-key
    @Resource
    private YuCongMingClient client;

    /**
     * AI 对话接口
     * @param message
     * @return
     */
    public String doChat(Long modelId, String message){
        // 构造请求
        DevChatRequest devChatRequest = new DevChatRequest();
        devChatRequest.setModelId(modelId);//使用的会话模型（助手）id，注册后获得
        devChatRequest.setMessage(message);//要发送的消息，不超过 1024 字

        // 获取响应
        BaseResponse<DevChatResponse> response = client.doChat(devChatRequest);// 接收ai返回的消息
        if (response == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "AI 响应错误");
        }
        return response.getData().getContent();
    }

}
